# -*- coding: utf-8 -*-
from Reader import Reader
import matplotlib.pyplot as plt
import matplotlib
# matplotlib.rc("font",family='FangSong')
matplotlib.rcParams['font.family'] = ['KaiTi', 'sans-serif']
matplotlib.rcParams['axes.unicode_minus'] = False
plt.style.use("./journal.mplstyle")
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.ticker import MultipleLocator
import config
import numpy as np
reader = Reader('data.xlsx')
with PdfPages('preview.pdf') as pdf:
    fig, ax = plt.subplots()
    ax.plot(reader.T, reader.P_GDP['P'], marker='o', label='总人口')
    ax.set_xlabel('年份')
    ax.set_ylabel('总人口/万人')
    ax.xaxis.set_major_locator(MultipleLocator(1))
    pdf.savefig(fig)
    plt.close()

    fig, ax = plt.subplots()
    ax.plot(reader.T, reader.P_GDP['GDP_t'], marker='o', label='GDP')
    ax1 = ax.twinx()
    ax1.plot(reader.T, reader.P_GDP['GDP_t']/reader.P_GDP['P'], color='r', label='人均GDP')
    ax.set_xlabel('年份')
    ax.xaxis.set_major_locator(MultipleLocator(1))
    ax.set_ylabel('GDP/亿元')
    ax1.set_ylabel('人均GDP[亿元/万人]')
    ax.legend()
    ax1.legend(loc='right')
    pdf.savefig(fig)
    plt.close()

    fig_axs = []
    colorlist = ['b', 'g', 'r', 'k', 'orange', 'violet']
    industryName = config.industryName[np.array([0,2,3,4,5])]
    industryMap = dict(zip(industryName, np.arange(5)))
    colorMap = dict(zip(config.energyName, colorlist))
    for i in range(5):
        fig, ax = plt.subplots()
        fig_axs.append((fig,ax))
    for d in reader.Carbon_Energy.columns:
        industry, resource = d.split('.')
        if not np.all(reader.Carbon_Energy[d]=='-'):
            ys = reader.Carbon_Energy[d]
            ys[ys=='-'] = np.nan
            fig_axs[industryMap[industry]][1].scatter(reader.T, ys, color=colorMap[resource], label=config.energyNameMap[resource])
    for d in industryName:
        ax = fig_axs[industryMap[d]][1]
        ax.set_xlabel('年份')
        ax.xaxis.set_major_locator(MultipleLocator(1))
        ax.set_ylabel('碳转换因子（'+config.industryNameMap[d]+')')
        ax.set_ylim([0,10])
        ax.legend()
        pdf.savefig(fig_axs[industryMap[d]][0])
    plt.close()

    fig_axs = []
    colorlist = ['b', 'g', 'r', 'k', 'orange', 'violet']
    industryName = config.industryName[np.array([0,2,3,4,5])]
    energyMap = dict(zip(config.energyName, np.arange(6)))
    colorMap = dict(zip(industryName, colorlist))
    for i in range(6):
        fig, ax = plt.subplots()
        fig_axs.append((fig,ax))
    for d in reader.Carbon_Energy.columns:
        industry, resource = d.split('.')
        if not np.all(reader.Carbon_Energy[d]=='-'):
            ys = reader.Carbon_Energy[d]
            ys[ys=='-'] = np.nan
            fig_axs[energyMap[resource]][1].scatter(reader.T, ys, color=colorMap[industry], label=config.industryNameMap[industry])
    for d in config.energyName:
        ax = fig_axs[energyMap[d]][1]
        ax.set_xlabel('年份')
        ax.xaxis.set_major_locator(MultipleLocator(1))
        ax.set_ylabel('碳转换因子（'+config.energyNameMap[d]+')')
        ax.legend()
        pdf.savefig(fig_axs[energyMap[d]][0])
    plt.close()

    for industry in config.industryName:
        fig, ax = plt.subplots()
        targetName = [industry + '.' +i for i in config.energyName]
        ys = reader.Energy[targetName].to_numpy().T
        ax.stackplot(reader.T, ys, labels=list(config.energyNameMap.values()))
        ax.set_xlabel('年份')
        ax.xaxis.set_major_locator(MultipleLocator(1))
        ax.set_ylabel('能源消耗（'+config.industryNameMap[industry]+')')
        ax.legend()
        pdf.savefig(fig)
        plt.close()
    
    for industry in config.industryName:
        fig, ax = plt.subplots()
        targetName = [industry + '.' +i for i in config.energyName]
        ys = reader.Energy[targetName].to_numpy().T
        ax.stackplot(reader.T, ys/np.sum(ys, axis=0), labels=list(config.energyNameMap.values()))
        ax.set_xlabel('年份')
        ax.xaxis.set_major_locator(MultipleLocator(1))
        ax.set_ylabel('能源消耗比例（'+config.industryNameMap[industry]+')')
        ax.legend()
        pdf.savefig(fig)
        plt.close()

    fig, ax = plt.subplots()
    industryName = config.industryName[np.array([0,2,3,4])]
    GDPs = reader.P_GDP[['GDP_'+i for i in industryName]].to_numpy()
    Energys = []
    for industry in industryName:
        targetName = [industry + '.' +i for i in config.energyName]
        Energys.append(np.sum(reader.Energy[targetName].to_numpy(), axis=1))
    Energys = np.array(Energys).T
    for i, industry in enumerate(industryName):
        ax.plot(reader.T, (Energys/GDPs)[:,i], marker='o', label=config.industryNameMap[industry])
    ax.set_xlabel('年份')
    ax.xaxis.set_major_locator(MultipleLocator(1))
    ax.set_ylabel('单位生产总值对应的能源消耗')
    ax.legend()
    pdf.savefig(fig)
    plt.close()

    fig, ax = plt.subplots()
    fig1, ax1 = plt.subplots()
    fig2, ax2 = plt.subplots()
    fig3, ax3 = plt.subplots()
    industryName = config.industryName[np.array([0,2,3,4])]
    GDPs = reader.P_GDP[['GDP_'+i for i in industryName]].to_numpy().T
    ax.stackplot(reader.T, GDPs, labels=[config.industryNameMap[industry] for industry in config.industryName[np.array([0,2,3,4])]])
    ax2.stackplot(reader.T, GDPs/np.sum(GDPs, axis=0), labels=[config.industryNameMap[industry] for industry in config.industryName[np.array([0,2,3,4])]])
    for i, industry in enumerate(industryName):
        ax1.plot(reader.T, GDPs[i,:]/reader.P_GDP['P'], marker='o', label=config.industryNameMap[industry])
        ax3.plot(reader.T, GDPs[i,:]/np.sum(GDPs, axis=0), marker='o', label=config.industryNameMap[industry])
    ax.set_xlabel('年份')
    ax.xaxis.set_major_locator(MultipleLocator(1))
    ax1.set_xlabel('年份')
    ax1.xaxis.set_major_locator(MultipleLocator(1))
    ax2.set_xlabel('年份')
    ax2.xaxis.set_major_locator(MultipleLocator(1))
    ax3.set_xlabel('年份')
    ax3.xaxis.set_major_locator(MultipleLocator(1))
    ax.set_ylabel('不同产业部门GDP')
    ax1.set_ylabel('不同产业部门人均GDP')
    ax2.set_ylabel('不同产业部门GDP比例')
    ax3.set_ylabel('不同产业部门GDP比例')
    ax.legend()
    ax1.legend()
    ax2.legend()
    ax3.legend()
    pdf.savefig(fig)
    pdf.savefig(fig1)
    pdf.savefig(fig2)
    pdf.savefig(fig3)
    plt.close()

    fig, ax = plt.subplots()
    fig1, ax1 = plt.subplots()
    industryName = config.industryName[np.array([0,2,3,4,5])]
    carbons = reader.Carbon[industryName].to_numpy().T
    ax.stackplot(reader.T, carbons, labels=[config.industryNameMap[industry] for industry in config.industryName[np.array([0,2,3,4, 5])]])
    ax1.stackplot(reader.T, carbons/np.sum(carbons, axis=0), labels=[config.industryNameMap[industry] for industry in config.industryName[np.array([0,2,3,4,5])]])
    ax.set_xlabel('年份')
    ax.xaxis.set_major_locator(MultipleLocator(1))
    ax1.set_xlabel('年份')
    ax1.xaxis.set_major_locator(MultipleLocator(1))
    ax.set_ylabel('不同产业部门碳排放量')
    ax1.set_ylabel('不同产业部门碳排放量比例')
    ax.legend()
    ax1.legend()
    pdf.savefig(fig)
    pdf.savefig(fig1)
    plt.close()